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 run --rm -p 80:80 -v $(pwd):/usr/share/nginx/html nginx:latest--rm: Automatically removes the container after the process is finished-p 80:80: This maps port 80 on the host machine (your computer) to port 80 on the container-v $(pwd):/usr/share/nginx/html: This mounts a volume from a directory on the host machine (your computer) to a directory in the container;$(pwd)is a shell command that returns the current directory of the host machine;/usr/share/nginx/htmlis default directory that Nginx serves files fromnginx:latest: This specifies the image to use (the default/official nginx image from Docker Hub)
Let's say you have this Ruby script version_check.rb:
# Print the current version of Ruby
puts "Current Ruby version: #{RUBY_VERSION}"You can run this in a ruby 3.3.4 container like so:
docker run --rm -v "$(pwd)":/usr/src/app -w /usr/src/app ruby:3.3.4 ruby version_check.rb-w /usr/src/app: Sets the working directory in the container to /usr/src/app
You could also do something like this so you don't have to add a working directory:
docker run --rm -v $(pwd):/app ruby:latest ruby /app/version_check.rbSay 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 nginx-with-htmlThis 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:latestThink 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 alpinedocker container run -p 8888:80 --rm nginxThis 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 nginxdocker container port 4ef58e0b2ef5
# 80/tcp -> 0.0.0.0:8888docker 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_lehmannNotice that there is a name and an ID number.
docker container ls -aIf 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 nginxdocker 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 processdocker rm <container-id-or-name>docker rm $(docker ps -a -q)docker image lsdocker container inspectThis 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 2The -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 bashdocker container run -it ubuntu:14.04 bashTo automatically remove the container when finished, use the --rm flag:
docker container run --rm -it ubuntu:14.04 bashdocker network ls
# => NETWORK ID NAME DRIVER SCOPE
# => 8c8c0be73fb5 bridge bridge local
# => 9e306b3b7093 host host local
# => 1cde17949134 none null localDocker 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 postgresYou 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 nginxTo remove all containers and images, including any stopped or unused containers and images:
docker system prune -aRemove all volumes:
docker volume pruneVerify that all is gone
docker container ls && docker images && docker volume ls