DEV Community

Cover image for Getting Started with Docker 🐋

Posted on

Getting Started with Docker 🐋

Hello everyone! This week I am going to give a brief tutorial on how to get started with Docker. See my last post for a conceptual breakdown of what containers are and when to use them. First thing is to make sure you install docker on your computer. Click the link for instructions for Mac, Windows, and Linux. Once everything is set up, you can run the hello world command to verify everything is working.

docker run hello-world
Enter fullscreen mode Exit fullscreen mode

If everything is good to go we will get started with a prebuilt busybox image. Busybox is an executable software suite that contains many Unix utilities.

docker pull busybox
Enter fullscreen mode Exit fullscreen mode

Next you tell docker to take the image and run it as a container. It will look like nothing is happening but docker is running the container behind the scenes.

docker run busybox
Enter fullscreen mode Exit fullscreen mode

In this case the container started but there were no commands passed to the container for busybox to execute so it exited without doing anything. Lets throw it an echo statement to see what it does.

docker run busybox echo “My name is busybox!”
Enter fullscreen mode Exit fullscreen mode

The next feature we are going to look at is the ps command. This is the tool used to see all of the containers that are currently running. Currently if we were to use it there would be nothing listed because the busybox container exits once it is done. If we add the -a option to it then it will all of the containers that have been ran.

Once you are done with a container you can remove it using the rm command followed by the container ID. Note: the container ID below is an example. You will have to look up yours with the ps command.

docker rm 305297d7a235
Enter fullscreen mode Exit fullscreen mode

Understanding the Docker terminology is important to reading up on the documentation for more complex projects.

  • Images: These are like the blueprints or a snapshot of the application that is going to be put into the container.

  • Containers: Containers are built using the corresponding image and works kind of like a virtual machine in concept. See my previous post for a more in depth discussion.

  • Docker Daemon: This is the background service that runs on the host machine. It manages building, running, and distributing them.

  • Docker Client: This is the command line tool that allows users to interface with the daemon.

  • Docker Hub: This is a registry of prebuilt docker images like the busybox one we used. You can pull prebuilt images of ubuntu, postgres, python, and many more.

The last thing I am going to discuss before wrapping up this post are Dockerfiles. Dockerfiles are a text file that is a series of commands that tells the client how to build the container. Depending on how complex your container is this can be simple. Below I will show the structure of a Dockerfile for a basic python flask app.

FROM python:3

# set a directory for the app
WORKDIR /usr/src/app

# copy all the files to the container
COPY . .

# install dependencies
RUN pip install --no-cache-dir -r requirements.txt

Enter fullscreen mode Exit fullscreen mode

The order of operations for this is that you first want to specify the image you are pulling FROM which in this case is Python. Next you specify the working directory and copy all of the files in it to the container. Once that is done you run pip install in the container to install any needed dependencies. For networking you expose port 5000 so that it can be accessed outside of the container. Lastly you run the CMD command to tell the container which command it should run and the file to run the command on.

From here you run the docker build command on the directory that contains the dockerfile which should be in the root folder of your project. See the example below.

docker build -t yourAppFolder .
Enter fullscreen mode Exit fullscreen mode

Well everyone, this was a guide on how to get started with Docker. There are many more things to learn especially if you want to start working with micro-services and have to orchestrate two or more containers for an application. I invite you to dig deeper and try building your own containers for an application. Let me know in the comments if you enjoyed this guide or like this post so that more people check it out. I hope you all have a great week and happy coding!





Top comments (1)

badreddine11 profile image