Create and run a new container from an image Copy 1
docker container run -it --rm ubuntu /bin/bash
-i
or --interactive
: Keep STDIN open even if not attached-t
or --tty
: Allocate a pseudo-TTY--rm
: Automatically remove the container when it exits--name
: Assign a name to the container-d
or --detach
: Run container in background and print container IDAliases: docker container run, docker run
List containers -a
or --all
: Show all containers (default shows just running)Aliases: docker container ls, docker container list, docker container ps, docker ps
Start or stop a container Copy 1
docker container stop container_name
Remove containers Copy 1
docker container rm -f $( docker container ps -aq)
-q
or --quiet
: Only display container IDsRun a container as a daemon: Copy 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 dirCopy 1
docker container inspect container_name
Write a basic Dockerfile: Copy 1
2
3
4
5
6
FROM # Set a base image
RUN # Execute a command in the container
ENV # Set an environment variable
WORKDIR # Set the working directory
VOLUME # Create a mount point for a volume
CMD # Set executable for the container
Build an image from a Dockerfile Copy 1
docker build -t image_name .
.
: Path to the context (.
- current working directory)-t
or --tag
: Name and optionally a tag (format: “name:tag”)-f
or --file
: Name of the Dockerfile (default: “context_path/Dockerfile”)Aliases: docker buildx build, docker buildx b
Access the Docker daemon as a non-root user Copy 1
2
groupadd docker
usermod -a -G docker $USER
Remove build cache Copy 1
2
3
4
# Check the disk usage of Docker
docker system df
# Remove Docker build cache
docker buildx prune -f
Rule of thumb 1 app = 1 container Processes 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 containers Happy containerizing! 🙂