1. Running Docker containers
Welcome! My name is Tim, and I'm a software engineer. In this course, we'll give you hands-on experience managing, running, and creating containers. Let's start by seeing how to use Docker to start, stop, and inspect the output of containers.
2. Prerequisite
We'll be using the command line extensively, and some familiarity is needed. Commands like nano will be used with only a brief introduction. Therefore, please take the prerequisite course if you're not already familiar with most shell commands here.
3. The Docker CLI
The Docker command line interface or CLI allows us to send instructions to the Docker daemon, which manages containers and images. The basic command is docker.
To start a container, we need an image, which acts as a blueprint defining what will be available in the container. For example, the ubuntu image contains the full Ubuntu OS. When we start a container from this image, we get a running instance of Ubuntu that we can interact with via a shell.
4. Docker container output
If we want to start a container from an image, we can use the docker run command, followed by the image-name.
To start the hello-world image, we would use docker run hello world.
By default, Docker starts a container and shows you its output while it's running. When we run a hello-world container, it prints an explanation of how the container works and then stops.
5. Choosing Docker container output
When an image is created, the creator can choose what happens when a container is started from the image. For example, the creators of the hello-world image chose to print out text and then make the container stop itself.
Another example is the Ubuntu image. When starting an Ubuntu container, it will start and then shut down immediately without printing any output. Its creators decided it didn't make sense to output anything specific by default.
6. An interactive Docker container
Instead, the Ubuntu image is intended to be used with the dash it flag. Using 'docker run dash it' followed by an image-name, we can start a container and simultaneously get an interactive shell in this container. If we do this with the Ubuntu image, we get a new shell inside the new container.
The shell gives us access to a clean Ubuntu environment isolated from our host machine because it runs inside the container.
Once we want to exit the container, we simply use the exit command, which returns us to the host machine and then stops the container.
7. Running a container detached
We saw a container that just prints text and one that makes more sense to use interactively. A third type of container processes data or can be interacted with in some way externally, for example, a container with a database like Postgres.
These are run using Docker run dash d, for detached, followed by the image-name. These containers run in the background without printing their output to our shell.
8. Listing and stopping running containers
The docker ps command allows us to see any running containers.
The first column contains the container-id, uniquely identifying each container.
The' docker stop' command can be used to stop containers we no longer need.
9. Summary of new commands
Here is a summary of the new commands you can refer to when completing the exercises.
10. Let's practice!
Now it's your turn to start and stop containers!