USER
- In linux machines, root user has full permissions,
-
By default when you build image the user is going to root.
-
Example
FROM alpine:3
CMD ["sleep", "1d"]
- docker exec command can run a command in a running container
docker exec test whoami
docker exec -it test1 /bin/sh
- It is recommended to run you application as a normal user
- As part of your docker image building create a user and switch to that user
- Dockerfile
FROM alpine:3
RUN adduser -D -h /app appuser
USER appuser
CMD ["sleep", "1d"]
WORKDIR
- WORKDIR instruction make a particular folder your current working directory
FROM alpine:3
RUN adduser -D -h /app appuser
USER appuser
WORKDIR /app
CMD ["sleep", "1d"]
Running docker contianers
- Playing with docker commands
- Figure out aliases dont memorize
docker images
docker image ls
docker ps
docker container ls
- A container can be run in two modes
- attached:
- background (detached)
- To run a docker command all we need to do is
docker run <image>
docker run -d <image>
-
When we create a container, each container gets a
- unique id
- unique name (if the name is not provided, docker generates a name)
-
To provide name
docker run -d --name <name> <image>

-
Name conflicts apply, two containers having same name
-
A container might have an application which is running on some port, we use portforwording for that
docker run -d -p 18000:80 --name <name> <image>


- To ensure we have dynamic portforwarding (When we have EXPOSE instruction)
docker run -d -P --name <name> <image>