Basic Docker Notes
To run a container from an image:
1docker container run -it --rm ubuntu /bin/bash
-t
: terminal inside
-i
: (STDIN) grabbing
--rm
: remove container automatically when process exits
--name
: name the container or daemon
-d
: daemonize the container running
To list all containers:
1docker container ps -a
To start or stop container:
1docker container stop container_name
To remove container(s):
1docker container rm -f $(docker container ps -aq)
-q
: print only container IDs
To run a container as a daemon:
1docker container run -d --name "test-nginx" -p 8080:80 -v $(pwd):/usr/share/nginx/html:ro nginx:latest
-p
: port mapping, host_port:container_port
-v
: volume mounting, host_dir:container_dir
$(pwd)
: current working dir
To check the information of a resource(container or image or volume or network):
1docker container inspect container_name
To write a basic Dockerfile:
1FROM #Set base image
2RUN #Execute command in container
3ENV #Set environment variable
4WORKDIR #Set working directory
5VOLUME #Create a mount point for a volume
6CMD #Set executable for container
To build an image from Dockerfile:
1docker build -t image_name .
.
: means context is the current working directory.
-t
: sets a name for the image
For accessing to the docker daemon as a non-root user:
1groupadd docker
2usermod -a -G docker $USER
Remove all unused(dangling) build cache:
1docker builder prune
Rule of thumb
- 1 app = 1 container
- Process should be running in the foreground
- Keep data in volumes, not in containers
- Do not use SSH, use
docker exec
instead - Avoid manual configurations inside container
Happy dockerizing, Gents! ๐