Getting to Know Docker

Mahdia Aliyya
6 min readJul 2, 2021

As a developer, there are times when we build code and it works absolutely fine on our computer. But then you send it to the tester or other developers. and the code wouldn’t work on their system. This may happen due to the differences in computer environments. And this is where Docker comes in handy. But what is Docker exactly?

What is Docker?

Docker is a tool that is used to automate the deployment applications in a lightweight container so the application can work efficiently in different environments. It’s important to note that the container is actually a software package that consists of all the dependencies required to run the application. So, multiple containers can run on the same hardware the containers are maintained in isolated environments. And the developer can be assured that the application will run on any other Linux machine regardless of any customized settings that machine might have that could differ from the machine used for writing and testing the code.

Benefits of Docker

Some of the benefits of using Docker are:

  • CI Efficiency — Docker enables you to build a container image and use that same image across every step of the deployment process. This allows it to separate non-dependent steps and run them in parallel. Thus, the length of time it takes from build to production can be sped up notably.
  • Compatibility and Maintainability — One of the benefits that the entire team will appreciate is parity. It means that your images run the same no matter which server or whose laptop they are running on. There’ll be less time spent setting up environments, debugging environment-specific issues, and a more portable and easy-to-set-up codebase. It also means your production infrastructure will be more reliable and easier to maintain.
  • Multi-Cloud Platforms — Docker containers can be run inside an Amazon EC2 instance, Google Compute Engine instance, Rackspace server, or VirtualBox, provided that the host OS supports Docker.
  • Isolation — Docker ensures your applications and resources are isolated and segregated. Docker makes sure each container has its own resources that are isolated from other containers. You can have various containers for separate applications running completely different stacks.
Docker Architecture

Simple Example of Docker Implementation — Build and run the todo sample app

This is part of a tutorial from Microsoft. Here we will be working with a simple todo list manager.

Get the App

Build the app’s container image

1. Create a file named Dockerfile in the same folder as the file package.json with the following contents.

// Dockerfile
FROM node:12-alpine
WORKDIR /app
COPY . .
RUN yarn install --production
CMD ["node", "/app/src/index.js"]

2. Open a terminal and go to the app directory with the Dockerfile. Now build the container image using the docker build command.

docker build -t getting-started .

Starting an app container

  1. Start your container using the docker run command and specify the name of the image you just created:
docker run -dp 3000:3000 getting-started

2. After a few seconds, open your web browser to http://localhost:3000. You should see the app!

Now you can add an item or two and see that it works as you expect. You can also mark items as complete and remove items. Your frontend is successfully storing items in the backend!

Update the source code

For example, we want to change the “empty text” when you don’t have any todo list items to “You have no todo items yet! Add one above!”. Let’s make the change.

  1. In the src/static/js/app.js file, update line 56 to use the new empty text.
  2. Build the updated version of the image, using the same command you used before.
  3. Start a new container using the updated code.

The new container couldn’t start, because your old container is still running. The reason this is a problem is because that container is using the host’s port 3000 and only one process on the machine (containers included) can listen to a specific port. To fix this, remove the old container.

Replace the old container

To remove a container, it first needs to be stopped. Once it has stopped, it can be removed.

1. Get the ID of the container by using the docker ps command.

docker ps

2. Use the docker stop command to stop the container.

# Swap out <the-container-id> with the ID from docker ps
docker stop <the-container-id>

3. Once the container has stopped, you can remove it by using the docker rm command.

docker rm <the-container-id>

4. Now, start your updated app.

5. Refresh your browser on http://localhost:3000 and you should see your updated help text!

Share your app

  1. Create a repo
    Login to Docker Hub and Click the Create Repository button. For the repo name, use getting-started. Make sure the Visibility is Public. Click the Create button!
    → If you look on the right-side of the page, you’ll see a section named Docker commands. This gives an example command that you will need to run to push to this repo.
  2. Push the image
  • Sign in to the Docker Hub using the command docker login -u <username>.
  • Use the docker tag command to give the getting-started image a new name. Be sure to swap out <username> with your Docker ID.
docker tag getting-started <username>/getting-started
  • Try your push command. If you’re copying the value from Docker Hub, you can drop the tagname portion, as you didn't add a tag to the image name. If you don't specify a tag, Docker will use a tag called latest.
docker push <username>/getting-started

3. Run the image on a new instance
Now that your image has been built and pushed into a registry, try running the app on a brand new instance that has never seen this container image!

  • Open your browser to Play with Docker.
  • Sign in with your Docker Hub account.
  • Once you’re logged in, click on the “+ ADD NEW INSTANCE” link in the left side bar. After a few seconds, a terminal window will be opened in your browser.
  • In the terminal, start your freshly pushed app.
docker run -dp 3000:3000 <username>/getting-started

you should see the image get pulled down and eventually start up!

  • Click on the 3000 badge when it comes up and you should see the app with your modifications! Hooray! If the 3000 badge doesn’t show up, you can click on the Open Port button and type in 3000.

Now we were able to build an update, we also learned how to share images by pushing them to a registry. Then we went to a brand new instance and were able to run the freshly pushed image. This is quite common in CI pipelines, where the pipeline will create the image and push it to a registry and then the production environment can use the latest version of the image.

--

--