DEV Community

Hiromi
Hiromi

Posted on

Leanings: Docker and it perks

After a long year of trying to dive into new topics and some failed certification attempts, I figured out that I needed more practice and motivating hands-on projects. Therefore, I decided to support a friend by building an application for his business.

Given that I had the power to choose all the steps and the structure of the project, I decided I investing a bit in my Cloud Engineering skills.

The project will be described here: Sushi Project (Coming soon).

The project will use a DigitalOcean Ubuntu Droplet (DO). To transport everything developed locally, I decided to create a Docker container and use GitHub Actions to transfer it to DO.

From this, I have some insights about a Docker writer/workflow that might help you in the future.

Installation via command line:

I am using Ubuntu, and I prefer to install everything via the command line. It gives me a better understanding of what is being done and—low key—helps me avoid breaking my system again.

I used the links [1] and [2] to install Docker and understand the process step-by-step. Below, I have documented the code from link [1] along with an explanation of each step:

# Add Docker's official GPG key:
# update the ubuntu
sudo apt update
# help package to support the installation
sudo apt install ca-certificates curl
# create a keyrings directory to add the trusted gpg keys (docker requirements)
sudo install -m 0755 -d /etc/apt/keyrings 
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
# adjust the permission to the .asc file
sudo chmod a+r /etc/apt/keyrings/docker.asc

# Add the repository to Apt sources:
sudo tee /etc/apt/sources.list.d/docker.sources <<EOF
Types: deb
URIs: https://download.docker.com/linux/ubuntu
Suites: $(. /etc/os-release && echo "${UBUNTU_CODENAME:-$VERSION_CODENAME}")
Components: stable
Signed-By: /etc/apt/keyrings/docker.asc
EOF
Enter fullscreen mode Exit fullscreen mode

After understanding better the code I was able to understand better the software itself.

But what is docker?

I’ll be honest: in the beginning, Docker sounded pretty confusing to me. I often mixed it up with GitHub concepts (I know, pretty dumb!) and didn't really understand the purpose. However, this blog post gave me a really good explanation.

My overall takeaway is this: Docker is a place where we can put all our code along with every installation needed for it to run smoothly anywhere. Just like a local project environment, we are able to transfer these installations online so that everyone can run the project exactly the same way on their own computer.

Amazing, don’t you think? However, as a fairly new "command line girl," I struggled a bit to create my first Docker container in Ubuntu.

So, here are some tips to make your life easier:

1) Basic concepts: understand the basics concepts of Docker.

  • Docker file: blueprint to create an image
  • Docker image: The template for running Docker containers.
  • container: A running process based on an image.

In other words: the Dockerfile is the idea of eating a cookie, the Image is the recipe, and the Container is the actual cookie after it’s been baked!

This video explain this very well. :)

2) Use the commands, this helps to understand better:

docker build -t  {image_name}
Enter fullscreen mode Exit fullscreen mode

This command build your image. The -t means tag, to create a human readable string.

docker images
Enter fullscreen mode Exit fullscreen mode

Return all the images that you built in your computer. This is very good to get the Image ID.

docker ps / docker ps -a
Enter fullscreen mode Exit fullscreen mode

Return all the current containers running. If you use the second command, return all the containers that ran and when they stopped. It's good to get the container id information.

docker run -d -p host_port:container_port {container}
Enter fullscreen mode Exit fullscreen mode

This command runs your container. The -d (detached) allows to run it in background, which is really useful to debug your container while is running. The -p is the publish, allowing an external traffic to reach the application. The host_port:container_port, are the ports that the container will run locally.

docker exec {container} ls -la {WORKDIR path}
Enter fullscreen mode Exit fullscreen mode

This command is really useful for me to confirm that all the files were copied correctly from my machine to the Docker container. It really helps me debug and verify my Dockerfile. Note that you can only run this command when your container is running in the background if you are using the command line.

3) It is essential to understand what is written in your Dockerfile and docker-compose.yml. These files act as the structural blueprint and the step-by-step instructions for running your container. Getting the structure and software versions right is the secret to making sure your container runs correctly every time.

If you are still feeling a little bit confused about how they differ, here is a great post explaining the difference between these two.

I hope blog post can help you. :)

Sources:
[1] https://docs.docker.com/engine/install/ubuntu/#install-using-the-repository
[2] https://www.youtube.com/watch?v=tjqd1Fxo6HQ
[3] https://aws.plainenglish.io/docker-explained-simply-for-a-10-year-old-the-magic-box-for-computer-programs-94452b930d6b
[4] https://www.youtube.com/watch?v=gAkwW2tuIqE

Top comments (0)