The Docker installation package available in the official Ubuntu repository may not be the latest version. To ensure we get the latest version, we’ll install Docker from the official Docker repository. To do that, we’ll add a new package source, add the GPG key from Docker to ensure the downloads are valid, and then install the package.
First, update your existing list of packages:
sudo apt update
Next, install a few prerequisite packages which let apt use packages over HTTPS:
sudo apt install apt-transport-https ca-certificates curl software-properties-common
Then add the GPG key for the official Docker repository to your system:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
Add the Docker repository to APT sources:
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable"
This will also update our package database with the Docker packages from the newly added repo. Make sure you are about to install from the Docker repo instead of the default Ubuntu repo:
apt-cache policy docker-ce
Finally, install Docker:
sudo apt install docker-ce
Docker should now be installed, the daemon started, and the process enabled to start on boot. Check that it’s running:
sudo systemctl status docker
sudo usermod -aG docker ${USER}
To apply the new group membership, log out of the server and back in, or type the following:
su - ${USER}
If you need to add a user to the docker group that you’re not logged in as, declare that username explicitly using:
sudo usermod -aG docker username
docker [option] [command] [arguments]
Outputattach Attach local standard input, output, and error streams to a running containerbuild Build an image from a Dockerfilecommit Create a new image from a container's changescp Copy files/folders between a container and the local filesystemcreate Create a new containerdiff Inspect changes to files or directories on a container's filesystemevents Get real time events from the serverexec Run a command in a running containerexport Export a container's filesystem as a tar archivehistory Show the history of an imageimages List imagesimport Import the contents from a tarball to create a filesystem imageinfo Display system-wide informationinspect Return low-level information on Docker objectskill Kill one or more running containersload Load an image from a tar archive or STDINlogin Log in to a Docker registrylogout Log out from a Docker registrylogs Fetch the logs of a containerpause Pause all processes within one or more containersport List port mappings or a specific mapping for the containerps List containerspull Pull an image or a repository from a registrypush Push an image or a repository to a registryrename Rename a containerrestart Restart one or more containersrm Remove one or more containersrmi Remove one or more imagesrun Run a command in a new containersave Save one or more images to a tar archive (streamed to STDOUT by default)search Search the Docker Hub for imagesstart Start one or more stopped containersstats Display a live stream of container(s) resource usage statisticsstop Stop one or more running containerstag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGEtop Display the running processes of a containerunpause Unpause all processes within one or more containersupdate Update configuration of one or more containersversion Show the Docker version informationwait Block until one or more containers stop, then print their exit codes
docker search ubuntu
docker pull ubuntu
docker images
The output will look similar to the following:
OutputREPOSITORY TAG IMAGE ID CREATED SIZEubuntu latest 1d622ef86b13 3 weeks ago 73.9MBhello-world latest bf756fb1ae65 4 months ago 13.3kB
As you’ll see later in this tutorial, images that you use to run containers can be modified and used to generate new images, which may then be uploaded (pushed is the technical term) to Docker Hub or other Docker registries. Let’s look at how to run containers in more detail.
The hello-world container you ran in the previous step is an example of a container that runs and exits after emitting a test message. Containers can be much more useful than that, and they can be interactive. After all, they are similar to virtual machines, only more resource-friendly.
As an example, let’s run a container using the latest image of Ubuntu. The combination of the -i and -t switches gives you interactive shell access into the container:
docker run -it ubuntu
After using Docker for a while, you’ll have many active (running) and inactive containers on your computer. To view the active ones, use:
docker ps
docker ps -a
To view the latest container you created, pass it the -l switch:
docker ps -l
To start a stopped container, use docker start, followed by the container ID or the container’s name.
docker start quizzical
To stop a running container, use docker stop, followed by the container ID or name.
docker stop quizzical
Once you’ve decided you no longer need a container anymore, remove it with the docker rm command, again using either the container ID or the name. Use the docker ps -a command to find the container ID or name for the container associated with the hello-world image and remove it.
docker rm youthful_curie
docker commit -m "What you did to the image" -a "Author Name" container_id repository/new_image_name
The -m switch is for the commit message that helps you and others know what changes you made, while -a is used to specify the author. The container_id is the one you noted earlier in the tutorial when you started the interactive Docker session. Unless you created additional repositories on Docker Hub, the repository is usually your Docker Hub username.
For example, for the user unuser, with the container ID of d9b100f2f636, the command would be:
docker commit -m "added Node.js" -a "unuser" d9b100f2f636 unuser/ubuntu-nodejs
When you commit an image, the new image is saved locally on your computer. Later in this tutorial, you’ll learn how to push an image to a Docker registry like Docker Hub so others can access it.
To push your image, first log into Docker Hub.
docker login -u docker-registry-username
Note: If your Docker registry username is different from the local username you used to create the image, you will have to tag your image with your registry username. For the example given in the last step, you would type:
docker tag unuser/ubuntu-nodejs docker-registry-username/ubuntu-nodejs
Then you may push your own image using:
docker push docker-registry-username/docker-image-name
To push the ubuntu-nodejs image to the unuser repository, the command would be:
docker push unuser/ubuntu-nodejs
To make sure you obtain the most updated stable version of Docker Compose, you’ll download this software from its official Github repository.
First, confirm the latest version available in their releases page. At the time of this writing, the most current stable version is 2.3.3.
Use the following command to download:
mkdir -p ~/.docker/cli-plugins/curl -SL https://github.com/docker/compose/releases/download/v2.3.3/docker-compose-linux-x86_64-o ~/.docker/cli-plugins/docker-compose
Next, set the correct permissions so that the docker compose command is executable:
chmod +x ~/.docker/cli-plugins/docker-compose
To verify that the installation was successful, you can run:
docker compose version
docker-compose.yml
FileStart off by creating a new directory in your home folder, and then moving into it:
mkdir ~/compose-democd ~/compose-demomkdir appnano app/index.html
Place the following content into this file:
<!doctype html><html lang="en"><head><meta charset="utf-8"><title>Docker Compose Demo</title><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/kognise/water.css@latest/dist/dark.min.css"></head><body><h1>This is a Docker Compose Demo Page.</h1><p>This content is being served by an Nginx container.</p></body></html>
Save and close the file when you’re done. If you are using nano, you can do that by typing CTRL+X, then Y and ENTER to confirm.
Next, create the docker-compose.yml file:
nano docker-compose.yml
Insert the following content in your docker-compose.yml file:
version: '3.7'services:web:image: nginx:alpineports:- "8000:80"volumes:- ./app:/usr/share/nginx/html
With the docker-compose.yml
file in place, you can now execute Docker Compose to bring your environment up. The following command will download the necessary Docker images, create a container for the web service, and run the containerized environment in background mode:
docker compose up -d
Your environment is now up and running in the background. To verify that the container is active, you can run:
docker compose ps
OutputName Command State Ports----------------------------------------------------------------------------------compose-demo_web_1 /docker-entrypoint.sh ngin ... Up 0.0.0.0:8000->80/tcp
To check the logs produced by your Nginx container, you can use the logs command:
docker compose logs
If you want to pause the environment execution without changing the current state of your containers, you can use:
docker compose pause
To resume execution after issuing a pause:
docker compose unpause
The stop command will terminate the container execution, but it won’t destroy any data associated with your containers:
docker compose stop
If you want to remove the containers, networks, and volumes associated with this containerized environment, use the down command:
docker compose down