To run a container from an image:

1
docker 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:

1
docker container ps -a

To start or stop container:

1
docker container stop container_name

To remove container(s):

1
docker container rm -f $(docker container ps -aq)

-q: print only container IDs

To run a container as a daemon:

1
docker 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):

1
docker container inspect container_name

To write a basic Dockerfile:

1
2
3
4
5
6
FROM        # Set base image
RUN         # Execute command in container
ENV         # Set environment variable
WORKDIR     # Set working directory
VOLUME      # Create a mount point for a volume
CMD         # Set executable for container

To build an image from Dockerfile:

1
docker 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:

1
2
groupadd docker
usermod -a -G docker $USER

Remove all unused(dangling) build cache:

1
docker 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 containerizing! 🙂