DEV Community

David Sabine
David Sabine

Posted on

Docker, Hello World

If you’ve never used Docker before, this is where to start.

Let’s say “Hello World!” in a Docker container.

Pre-requisites: Ensure Docker is Installed & Working

It’s important you have Docker Engine and Docker CLI and Docker Compose. How to know if you have these on your system?

Note: Docker Desktop for Linux/Mac/Windows includes all the necessary tools.

You’ll know whether these tools are installed and working if, at any command prompt, the following commands will report the version number of each tool.

docker --version

My system responds with:
> Docker version 20.10.17, build 100c701
Enter fullscreen mode Exit fullscreen mode
docker-compose --version

My system responds with:
>Docker Compose version v2.10.2
Enter fullscreen mode Exit fullscreen mode

Let’s make some containers from an existing image, then remove them from our system.

It’s helpful to know a few basics to keep your environment clean.

  1. docker pull hello-world: Build a simple image in a docker container and run a “Hello World” application.

  2. docker images: List all your images. You’ll see a new image, called hello-world is added to your local collection.

  3. docker run hello-world: Run the app within the container.

  4. docker container ls -a: List all docker containers.

It’s important to know that a new container is produced every time you run the app. For example:

  1. Run #3 above, again.

  2. Then run #4 above, again, and notice a new container in the list.

(To prevent the creation of a new container every time you want to interact with the app, there are ways to execute commands in a running container, et cetera. Those techniques are not described here.)

It's important to know how to clean up your environment.

(These commands and more are described in the “Complete Guide for Removing Docker Images” by Abhishek Prakash.)

  1. docker container ls -a: List all your containers. (Note the names or container ids.)

  2. docker rm boring_hoover: Remove a container with name “boring_hoover”.

  3. docker image rm hello-world: This will remove the hello-world image created in #1 above. Note this will fail if any containers exist using that image. (So remove those images first.)

Takeaways

  • Using docker, you downloaded an image, created containers from that image, and ran a program on that image.

  • Then you cleaned up the Docker artifacts on your system: the containers and the new image.

See also

My next article demonstrates how to do all this with docker-compose.

Top comments (0)