Let's say you have an index.html
file in the current directory:
<!DOCTYPE html>
<html>
<head>
<title>Hello Docker</title>
</head>
<body>
<h1>Hello, Docker!</h1>
</body>
</html>
You can run the official Docker nginx image and serve the file with:
$ docker container run --rm -p 80:80 -v $(pwd):/usr/share/nginx/html nginx
Say you have a directory with a Dockerfile
and an index.html
file.
Dockerfile
FROM nginx:latest
WORKDIR /usr/share/nginx/html
COPY index.html index.html
This says to use the latest version of the official nginx image from the Docker Hub.
It makes the working directory, or the directory that it begins it's process in /usr/share/nginx/html
.
And, then it copies over the following file:
index.html
<!DOCTYPE html>
<html>
<head>
<title>Hello Docker</title>
</head>
<body>
<h1>Hello, Docker!</h1>
</body>
</html>
Build the image:
$ docker image build -t nginx-with-html .
This builds the image from the Dockerfile in the current directory and tags it with the name nginx-with-html
.
Run a container of the image:
$ docker container run -p 80:80 --rm
This runs a container off of the image, connecting port 80 on the host machine to port 80 on the container. The --rm
flag will remove the running container after we exit with ctrl+z
.
If you want to upload this image to Docker Hub, you need to tag it:
$ docker image tag nginx-with-html:latest elliotlarson/nginx-with-html:latest
Think of tags as a composite of <image-name>:<repository-name>
.
If you are using oh-my-zsh, then you can just use the docker plugin:
In your .zshrc
file, make sure the plugins line has:
plugins+=(docker)
This will pull down the latest image:
$ docker pull alpine
$ docker container run -p 8888:80 nginx
This will download the nginx latest from the Docker Hub, or use a local version if found, and run it. You should now be able to navigate to http://localhost to view the vanilla nginx container running.
When you view the page, you will see the log output in your terminal.
The -p
flag connects port 8888 on the host machine to port 80 in the container.
$ docker container run --rm -p 8888:80 --name webhost nginx
$ docker container port 4ef58e0b2ef5
# 80/tcp -> 0.0.0.0:8888
$ docker container ls
# CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
# 4ef58e0b2ef5 nginx "nginx -g 'daemon of…" 23 seconds ago Up 21 seconds 0.0.0.0:8888->80/tcp objective_lehmann
Notice that there is a name and an ID number.
$ docker container ls -a
If you don't want the process to take up your terminal process, you can run the container in detached mode:
$ docker container run -p 8888:80 --detach nginx
# or
$ docker container run -p 8888:80 -d nginx
$ docker container logs <container-id-or-name>
$ docker container stop <container-id-or-name>
$ docker top <container-id-or-name>
For example:
# Run the latest nginx image
$ docker container run -p 80:80 nginx
# Get the container name/id
$ docker container ls
# CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
# 8b83c50abdc0 nginx "nginx -g 'daemon of…" 7 seconds ago Up 6 seconds 0.0.0.0:80->80/tcp eloquent_yonath
# Get the processes running on the nginx container
$ docker container top eloquent_yonath
# PID USER TIME COMMAND
# 79694 root 0:00 nginx: master process nginx -g daemon off;
# 79735 101 0:00 nginx: worker process
$ docker rm <container-id-or-name>
$ docker rm $(docker ps -a -q)
$ docker image ls
$ docker container inspect
This allows you to see memory usage, memory limit, and CPU usage.
$ docker container stats
# CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
# 8b83c50abdc0 eloquent_yonath 0.00% 2.09MiB / 1.952GiB 0.10% 21.4kB / 3.7kB 6.57MB / 0B 2
The -t
flag opens a tty prompt and the -i
command keeps the prompt active. bash
is the command to run once the prompt has been established.
$ docker container exec -it nginx bash
$ docker container run -it ubuntu:14.04 bash
To automatically remove the container when finished, use the --rm
flag:
$ docker container run --rm -it ubuntu:14.04 bash
$ docker network ls
# => NETWORK ID NAME DRIVER SCOPE
# => 8c8c0be73fb5 bridge bridge local
# => 9e306b3b7093 host host local
# => 1cde17949134 none null local
Docker has a default network subnet called bridge
that connects the local host machine to the docker containers.
This will give more detailed info about the network and give information about the containers that are connected
$ docker network inspect <network-id>
$ docker network connect <network-id> <container-id>
$ docker network disconnect <network-id> <container-id>
You can create a named volume that stores the data from a container to the host machine. Docker places it in some Docker owned directory on the host machine.
$ docker container run -d --name psql -v psql:/var/lib/postgresql/data postgres
You can map a directory in the container to a directory on the host machine.
$ docker container run -d --name nginx -p 80:80 -v $(pwd):/usr/share/nginx/html nginx
To remove all containers and images, including any stopped or unused containers and images:
$ docker system prune -a
Remove all volumes:
$ docker volume prune
Verify that all is gone
$ docker container ls && docker images && docker volume ls