How to run the docker in development mode

Hi,
I have create the custom docker image. Now whenever I make the change in code, I have to again build the custom image.
Please share your thoughts if anyone knows how to run the docker in development mode so that I can get the changes without building the docker image again.

Thanks & Regards,
Abhilash

I am not exactly sure what “docker in development mode” is supposed to be, but I guess you are looking for using a volume-bind to mount a host folder into a container folder for hot code deployment.

Note: if you bind a host folder into a container folder, the content of the host folder will eclipse the original content of the container folder. You will need to make sure that the host folder has all the required files in it, because otherwise your container might fail to start.

Note: due to a lack of provided information, I can not tell you which host folder to map into which container folder. This highly depends on the programming language used, the image design and wether your language/runtime even supports hot code deployment. If it doesn’t support hot code deployment you will still need to restart the container.

1 Like

Hi Meyay,
I am using python as a programming language. Development mode means every time we made the change in the code we suppose not to run “docker-compose up --build” docker command. It should take that changes. If we run the “docker-compose up --build” command again it will take some time to build the containers/Image.
I have already doing volume binding, still I need to run the build command every time.
I tried with “docker-compose up” command as well but changes are not getting reflecting without running build command.
Please suggest any inputs on this.
Thanks & Regards,
Abhilash

You need to wait till someone comes around who actually writes code in python. I can’t tell you if python does hot code replacement/deployment when the application is still running.

Abhilash, this should be possible when your Python code is mounted as a volume, and not packaged into the container image during build. As Metin said above, in this way the changes you make are reflected immediately in the container.

I’m not an expert in Python, but here’s one post I found doing this: How to Develop Your Python Docker Applications Faster - DZone Microservices

Also when using docker-compose you shouldn’t need to rebuild all of the containers. Just the one with the relevant Python code that has changed. Try “docker-compose build --no-cache pythonApp” for something like that.

1 Like

Hi @abhilash7878, Here is a little more detail, and an example, on the technique that that @brianwagner was suggesting:

  1. First, in your docker-compose.yaml file you want to mount your current directory as the WORKDIR from your Dockerfile. For example if your Dockerfile contains this statement:

    WORKDIR /app
    

    Then in your docker-compose.yaml file you want to add this:

        volumes:
          - .:/app
    

    That will mount the current folder '.' inside the container as /app.

  2. Next, you want to use container_name to give the container you want to develop in a well known name like this:

        container_name: development
    
  3. Finally, bring up the environment and use docker exec to exec into the running container like this:

    $ docker compose up -d
    $ docker exec -it development bash
    

This will place you inside the container in a bash shell with the current directory which contains your live code mounted under /app. Now you can make changes to the code on your computer with your favorite editor, and run the code inside of the docker container, and never have to rebuild while you are developing.

Here is a sample docker-compose.yaml file that implements this technique and starts a PostgreSQL sever to use in development:

---
version: "3"

services:
  app:
    build: .
    hostname: dev
    container_name: development
    volumes:
      - .:/app
    command: sleep infinity
    environment:
      DATABASE_URI: postgresql://postgres:s3cr3t@postgres:5432/postgres
    networks:
      - dev
    depends_on:
      - postgres

  postgres:
    image: postgres:alpine
    environment:
      POSTGRES_PASSWORD: s3cr3t
    volumes:
      - postgres:/var/lib/postgresql/data
    networks:
      - dev

volumes:
  postgres:

networks:
  dev:

Feel free to ask any questions. The key is to map your WORKDIR from the Dockerfile to your current folder as a volume inside the docker-compose.yaml file. (In this example the WORKDIR is /app). When you want to really run the code inside the container, simply comment out the volume mount and rebuild the container image.

Hope this helps.

Hi Rofrano. I face same issue docker in the development which faced abhilash7878. but now we read your all reply and solve our problem. thanks for telling and solve our problems. i know you time is more important so, thank you so much

Hi @abiijoy, You are very welcome. I’m glad my solution helped you. It was time well spent as long as it helps someone. Thanks for letting me know,

Thanks a lot dear Rofrano.