Streamline Your Local Development with Docker and Docker Compose: A Step-by-Step Guide

docker

Docker is a tool that allows you to run isolated environments, called containers, on your local machine. This makes it easy to develop and test your code in a consistent and reproducible way, regardless of your host operating system.

To use Docker for local development, you’ll first need to install it on your machine. Once that’s done, you can start by creating a new container from a pre-existing image. For example, if you’re developing a web application, you might use a base image that includes a web server like Apache or Nginx.

To mount your code to the container, you can use the -v option when running the docker run command. This tells Docker to mount a host directory to a specific location within the container. For example, the following command will mount the /path/to/local/code directory to the /var/www/html directory within the container:

docker run -v /path/to/local/code:/var/www/html -p 80:80 -it my-web-image

You can use environment variables to configure your container in a flexible and secure way. These variables can be passed to the container using the -e option when running the docker run command. For example, the following command will pass the DATABASE_URL variable to the container:

docker run -e DATABASE_URL=postgres://user:password@host:port/dbname -it my-web-image

You can also use a .env file to store your environment variables and mount it to the container using the -v option.

docker run -v /path/to/local/.env:/app/.env -it my-web-image

Once the container is running, you can use docker exec command to run commands inside the container.

docker exec -it container-name bash

Docker compose

Docker Compose is a tool that allows you to define and run multi-container applications using a single configuration file. It simplifies the process of setting up and running multiple containers by allowing you to define all the necessary configurations in a single file, called docker-compose.yml.

To use Docker Compose for local development, you’ll first need to install it on your machine. Once that’s done, you can create a new docker-compose.yml file in the root of your project. This file should define the services that make up your application, along with any necessary configurations.

For example, if you’re developing a web application that uses a MySQL database, your docker-compose.yml file might look something like this:

version: '3'
services:
  web:
    image: my-web-image
    ports:
      - "80:80"
    volumes:
      - "./code:/var/www/html"
    environment:
      - DATABASE_URL=mysql://user:password@db:3306/dbname
  db:
    image: mysql:5.7
    environment:
      - MYSQL_ROOT_PASSWORD=password
      - MYSQL_DATABASE=dbname
      - MYSQL_USER=user
      - MYSQL_PASSWORD=password

This configuration file defines two services: web and db. The web service uses the my-web-image image and maps port 80 on the host to port 80 in the container. It also mounts the ./code directory on the host to the /var/www/html directory in the container and sets the DATABASE_URL environment variable. The db service uses the mysql:5.7 image and sets environment variables for the root password, database name, and user credentials.

Once you have your docker-compose.yml file set up, you can use the docker-compose up command to start all the defined services.

docker-compose up

You can also use docker-compose down command to stop and remove the containers, networks, volumes, and images created by up.

docker-compose down

You can also use docker-compose exec command to run commands inside a container.

docker-compose exec web bash

You can also run Docker compose with the daemon flag, so that you can use the same terminal process for other things.

docker-compose -d up

Docker and Docker Compose allow you to easily set up and run multi-container applications. They simplify the process of configuring and running multiple containers and makes it easy to develop and test your code in a consistent and reproducible way. You can also use your existing .env file, to set your environment variables instead of hardcoding them in the compose file.

Fuse Web can help

Docker is a powerful platform with lots of advantages for businesses, but it can also be daunting to use. The process of containerizing apps and maintaining containers can be complicated and time-consuming for many businesses. Furthermore, businesses may be worried about the security and scalability of their containerized apps, and they may even lack the skills or resources to operate their Docker environment successfully.

Fuse Web can assist businesses in overcoming these challenges by offering professional guidance and support for their Docker-based initiatives. Our team has considerable Docker knowledge and can assist businesses with containerizing their apps, managing their containers, and optimizing their Docker environment for speed and scalability. Don’t hesitate, contact us now to see how we can help.

Related content