Installation
Install Docker on Linux:
curl -fsSL https://get.docker.com -o get-docker.sh
To use Docker as a non-root user, consider adding the user to the docker group:
sudo usermod -aG docker <username>
Download docker-compose:
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
Check docker-compose version:
docker-compose version
TIP: For auto-completion,
docker
+ Tab Key
Versions
Docker versions are YY.MM
based.
Edge -> monthly release and one month support
Stable -> quarterly release and 4 months support
Containers
Check version of client and server (engine):
docker version
More information about the client and server:
docker info
Management commands
docker <command> <sub-command> (options)
Image vs. Container
Image is the app to run
Container is an instance of that image running as a process. There can be many containers.
Docker's default image registry is Docker Hub.
Starting an Nginx server
Download an image from Docker Hub and start a container from that image:
docker container run --publish 80:80 nginx
The command docker container run
does the following:
- Looks for the image locally in the image cache
- If not found locally, it looks for the same in the remote image repository which defaults to Docker Hub and downloads the latest version
- Creates a container based on that image and prepares to start
- Gives the container a virtual IP on a private network inside the Docker engine
- Opens up port 80 on the host and forwards all requests to port 80 in the container
- Starts container by using the CMD instruction command in the image's Dockerfile
đź’ˇ Instruction commands
There are three types of instructions (commands) that we can use to build and run Dockerfiles:
RUN - Mainly used to build images and install applications and packages. Builds a new layer over an existing image by committing the results.
CMD - Sets default parameters that can be overridden from the Docker CLI when a container is running.
ENTRYPOINT - Default parameters that cannot be overridden when Docker Containers run with CLI parameters.
Running container in the background:
docker container run --publish <port-host>:<port-container> --detach <image>
# Example
docker container run --publish 80:80 --detach nginx
Give a name to the container:
docker container run -p 80:80 -d --name webhost nginx
-p
is short for--publish
and-d
is short for--detach
List all containers:
docker container ls -a
Short version:
docker ps -a
List only running containers:
docker container ls
Short version:
docker ps
Stop a container:
docker container stop <first-3-digits-container-id>
Get logs for a running container:
docker container logs <container-name>
Display the running processes of a container:
docker container top <container-name>
Remove one or more containers:
docker container rm <space-separated-3-digit-container-ids>
Force remove running containers:
docker container rm -f <space-separated-3-digit-container-ids>
Show all running processes:
ps aux
Use environment variables:
docker container run --publish 3307:3307 --detach --name database --env MYSQL_RANDOM_ROOT_PASSWORD=yes mysql
Get details of container config (metadata in JSON format):
docker container inspect <container-name>
Get live performance stats for all containers:
docker container stats
Get performance stats for a container:
docker container stats <container-name>
TIP - To see all options for a command, do
docker <command> <sub-command> --help
Get a shell inside containers
Start a new container interactively:
docker container run -it --name <container-name> <image> <cmd>
# Example
docker container run -it --name nginx nginx bash
# Default CMD for Ubuntu image is bash
docker container run -it --name ubuntu ubuntu
-it
is two different options:
-t
-> allocates Pseudo-TTY, simulates a real terminal
-i
-> keeps interactive session open to receive terminal input
Start an existing container (stopped earlier) interactively:
docker container start -ai <container-name>
-a
-> attach
-i
-> interactive shell
Run additional command in an existing running container:
docker container exec -it <container-name> <cmd>
Download an image:
docker pull <image>
List all images:
docker image ls
Top comments (0)