docker,

How to create a Docker image from a running container

Nov 26, 2022 · 1 min read · Post a comment

If you find yourself connecting to a Docker container, so you could investigate an issue, update a .conf file, or whatever other reason, you are doing an anti-pattern. However, if you really want to create a “snapshot” out of a running Docker container, you need to create a Docker image first. Thus, here are the steps required to do so.

Prerequisites

  • Docker

Solution

Step 1. List, find and copy the Docker container name. For example:

docker ps -a

Step 2. Commit the container.

docker commit redis_test

Note: The container doesn’t really need to be running though. Could be stopped as well, known as an Exited state.

Step 3. Tag the Docker image. For instance:

docker tag f2576d0b2d25 devcoops/redis_test:latest

Note(s):

  • Replace devcoops with your actual Docker repository hosting provider username, if any. The format usually goes something as: <docker_username>/<docker_repo_name>. For instance: devcoops/redis. A corresponding AWS example would be: <some_AWS_ID>/dkr.ecr.eu-west-1.amazonaws.com/<docker_repo_name>. If not, pick some name of your choice.
  • To commit and tag the Docker image with a single command, run:
    docker commit redis_test devcoops/redis_test:latest
    

Step 4. Confirm the Docker image.

docker images

Step 5. (Optional) Stop and remove the old container.

docker stop redis_test && docker rm redis_test

Step 6. Spin up a new container from the recently created image.

docker run --it -d devcoops/redis_test:latest

Conclusion

Feel free to leave a comment below and if you find this tutorial useful, follow our official channel on Telegram.