How to Create a Docker Image From a Container
I wanted to bake in a Database into the MySQL docker image on hub.docker.com.
1. Dockerfile
FROM mysql:5.7 # Add a database ENV MYSQL_DATABASE my_database COPY ./sql/ /docker-entrypoint-initdb.d/ RUN docker-entrypoint.sh CMD ["mysqld"]
2. Build. the container
> docker build --force-rm -t my_docker_account/mysql:5.7-baked-in-db ./Dockerfile
3. Run the container
We need to run the container so the MySQL image will import the SQL files and the database. Note “–name my_mysql“, we’re giving our container a nickname here which we will refer to later on.
> docker run --rm -d -p 3306:3306 --name my_mysql -e MYSQL_ROOT_PASSWORD=secret my_docker_account/mysql:5.7-baked-in-db
4. Log into the box (optional)
You can log into the box to have a look around if you like
> docker exec -it my_mysql /bin/bash
5. Use the container ID and push to the image namespace
> docker commit my_mysql my_docker_account/mysql:5.7-baked-in-db > docker push my_docker_account/mysql:5.7-baked-in-db
Now you have a pushed pre-built MySQL container as an image.
Gareth
Related Posts |
---|
Docker and Docker Compose |
Docker quick-tips |