Names, ports and settings
Name a container, reach it from your computer and pass it settings.
A Docker practice simulator runs inside your browser tab. It behaves like the real commands for what the lessons teach, and it is labelled as a simulation.
Give containers a name
Docker gives every container a random name unless you choose one with --name. A chosen name makes later commands easier to write and to read.
Names must be unique. Starting a second container with a name that is already used fails, even if the first one has stopped.
docker run -d --name cache redis
docker psPublishing a port
A container has its own network. A web server inside it listens on port 80, but your computer cannot reach that port until you publish it.
-p 8080:80 connects port 8080 on your computer to port 80 in the container. The order is always your computer first, then the container.
Two running containers cannot use the same port on your computer.
docker run -d --name web -p 8080:80 nginx
docker psYour turn: name and port
Options go before the image name.
Practice task
Run nginx in the background, named web, with port 8080 on your computer connected to port 80 in the container.
Your answer is run and checked in the lesson. Create a free account to try it.
Passing settings with -e
Many images are configured through environment variables. -e NAME=value sets one inside the container.
The postgres image needs a password before it will start. Without POSTGRES_PASSWORD the container stops straight away, and docker logs tells you why.
docker run -d --name db -e POSTGRES_PASSWORD=secret postgres
docker psYour turn: a database with a password
The container should be running, not stopped.
Practice task
Run postgres in the background, named db, with the setting POSTGRES_PASSWORD=learn.
Your answer is run and checked in the lesson. Create a free account to try it.