Mastering Docker: 10 Advanced Interview Questions and Answers

TLDR: This blog post covers 10 advanced Docker interview questions and answers, providing insights into Docker networking, Dockerfiles, image building, troubleshooting, and best practices for using Docker effectively in a DevOps environment.

In today's session, we will explore some advanced coding interview questions that you can expect during a Docker-focused DevOps interview. Whether you are preparing for an interview or looking to enhance your Docker skills, these questions are designed to deepen your understanding of Docker.

1. How do you create a Docker Network and connect multiple containers to it?

To create a custom Docker network and connect multiple containers, follow these steps:

  1. Ensure Docker is running. If not, install it using the command yum install docker.

  2. Start the Docker service with service docker start.

  3. Create a network using the command:

     docker network create custom_network
    
  4. List the networks with:

     docker network ls
    
  5. Run containers in the created network:

     docker run -dit --name alpine1 --network custom_network alpine
     docker run -dit --name alpine2 --network custom_network alpine
    
  6. Inspect the network to confirm both containers are connected:

     docker network inspect custom_network
    
  7. Test connectivity between containers using ping.

2. Write a Dockerfile that installs a specific version of Node.js and sets an environment variable.

To create a Dockerfile for Node.js version 14 and set an environment variable, use the following:

FROM node:14-alpine
ENV NODE_ENV=production
WORKDIR /app
COPY package.json .
RUN npm install
COPY . .
CMD ["node", "app.js"]

3. How do you handle building an image using a remote Git repository?

To build a Docker image from a Dockerfile in a remote Git repository, use:

docker build -t git_image https://github.com/username/repo.git

Ensure Git is installed on your machine.

4. Write a Docker Compose file that uses environment variables from an .env file.

Create a docker-compose.yml file:

version: '3'
services:
  app:
    image: node:14
    env_file:
      - .env
    ports:
      - "3000:3000"

And create an .env file:

NODE_ENV=production

5. How do you cache Docker layers for faster builds in a Dockerfile?

To optimize Docker builds, order your Dockerfile instructions from least frequently changing to most frequently changing. For example:

FROM node:14-alpine
COPY package.json .
RUN npm install
COPY . .

This way, if the application code changes, only the last layer will be rebuilt, speeding up the process.

6. How can you troubleshoot and debug a Docker container that is not starting?

If a container fails to start, you can:

  1. Check the logs using:

     docker logs container_name
    
  2. Start the container in interactive mode to troubleshoot:

     docker run -it --entrypoint /bin/sh image_name
    
  3. Inspect the container configuration:

     docker inspect container_id
    

7. Write a Dockerfile that uses ARG and ENV instructions to pass build-time and runtime variables.

Here’s an example Dockerfile:

FROM node:14-alpine
ARG NODE_VERSION=14
ENV NODE_ENV=production
RUN npm install -g node@${NODE_VERSION}

You can override the ARG during build time:

docker build --build-arg NODE_VERSION=16 -t my_node_image .

8. How do you remove all dangling images in Docker?

To remove dangling images, use:

docker image prune -f

This command will free up space by deleting unused images.

9. Write a Docker command to restart a container automatically if it fails.

To set a container to restart automatically on failure, use:

docker run --restart on-failure:5 image_name

This command will attempt to restart the container up to five times if it fails.

10. How do you set up a Docker container to run as a non-root user?

To run a container as a non-root user, create a user in your Dockerfile:

FROM node:14-alpine
RUN addgroup -S nodeapp && adduser -S nodeuser -G nodeapp
USER nodeuser

This ensures that subsequent instructions in the Dockerfile run as the specified non-root user.

Conclusion

These advanced Docker interview questions cover essential concepts and practices that can help you excel in your DevOps interviews. Understanding these topics will not only prepare you for interviews but also enhance your Docker skills for real-world applications. If you found this information helpful, consider subscribing for more content on Docker and DevOps practices.