Inspired by https://training.play-with-docker.com/beginner-linux/
This section contains a list of useful docker commands. It is only for reference. You don't have to type them (yet).
Parameters in angle brackets are replaced by user input.
Example: <image>
Run a shell from a container image.
docker run -it --rm <image> sh| Flag | Meaning |
|---|---|
| -it | Interactive |
| --rm | Remove container and volumes on exit |
<image> |
Name of image like ubungu, alpine etc. |
| sh | Command to execute. sh is a primitive shell |
Show running containers.
docker psShow all containers, even those that are not actively running.
docker ps -adocker exec -it <id or name> shYou can find ID and Name with docker ps command.
docker logs <id or name>docker build <directory>Where <directory> is a folder containing a Dockerfile.
It will create an image with a long HEX code as name.
You can name and version an image by tagging it.
docker build --tag <name>:<version> <directory>To stop a running container, do:
docker stop <id or name>You can remove a container once it is no longer up (running).
docker rm <id or name>To free up space on your computer, you can remove all unused objects (including volumes).
docker system prune --volumesWarning: can be dangerous since it deletes all data that is stored within containers.
Containers are based on a base image. Different base images exists for different Linux distributions.
On most Linux systems you can find out what distribution and version it is by
running cat /etc/issue.
Let's try it out on a couple of different base images.
From now on, you should try the commands out in your shell.
docker run -it --rm ubuntu shThen:
cat /etc/issueCTRL+d to exit out of the container. You should then see your normal prompt.
Many examples use Alpine Linux as base image since it has a small footprint. Let's try it out!
docker run -it --rm alpine shThen check the version with:
cat /etc/issueCTRL+d to exit out of the container.
You can also run a specific version of Alpine.
docker run -it --rm alpine:3.17 sh
cat /etc/issueCTRL+d to exit.
Let's put some of the Docker commands to use by spinning up a database.
docker run --name mydb -e POSTGRES_PASSWORD=mysecret -p 5432:5432 -d postgres| Parameter | Meaning |
|---|---|
| --name | Give the container a name (mydb) |
| -e | Set an environment variable for the container |
| -d | Run in background (short for "detach") |
You now have a PostgreSQL database running within a docker container. PostgreSQL (or postgres for short) is the database system you will be using throughout this semester.
You could have started MySQL database instead by running docker run --name mydb -e MYSQL_ROOT_PASSWORD=secret -d mysql.
With the power of container you are able to run a bunch of different services without installing anything directly on your host OS. This becomes even more powerful when you have projects that rely on different versions of the same database engine.
You can use the psql command from within the container to configure and query
the database.
Try it out!
docker exec -it mydb psql -U postgres postgresYou should see a postgres=# prompt.
You can find the version of postgres that is running with:
SELECT version();Then CTRL+d to exit. You should see your normal command line prompt again.
If the database had tables you would have been able to run queries against it.
In this part you will start a simple web server in a container.
Open a terminal. Navigate to a folder where you can store some temporary files for this guide.
Clone a sample repository.
git clone https://github.com/dockersamples/linux_tweet_app
cd linux_tweet_appThe repository comes with a Dockerfile, which is a recipe for a container image.
To view it type:
cat DockerfileIt uses the latest version of nginx as its base image. The only thing the recipe does is:
- Copy
index.htmlandindex.pnginto the container images - Expose the defaults ports for HTTP and HTTPS (web traffic)
- Start nginx
To build an image from the Dockerfile you simply run:
docker build .Notice the . (dot)
The name of image is the hex value you see right after writing image sha256:
in the output.
Not very friendly, so let's give it a tag.
docker build --tag linux_tweet_app:1.0 .You can then run a container from the image, using:
docker run -d -p 80:80 --name linux_tweet_app linux_tweet_app:1.0The reason you see linux_tweet_app twice in the command is because we name
the container linux_tweet_app and the image we use for the container is
tagged linux_tweet_app:1.0.
Didn't bother creating my own app for this, so just used one from somebody else.
To clean up, you first need to stop any active containers.
docker psThen for each, do:
docker stop <id>Or, if you are in a bash shell (like git-bash), then you can just do docker stop $(docker ps -a -q) to list and stop all containers.
To remove all stopped containers and free up space, do:
docker system prune