I'm using docker-compose to create my development environment. I want to build a specific image, but I don't know how to set a name for that image.
wildfly:
build: /path/to/dir/Dockerfile
container_name: wildfly_server
ports:
- 9990:9990
- 80:8080
environment:
- MYSQL_HOST=mysql_server
- MONGO_HOST=mongo_server
- ELASTIC_HOST=elasticsearch_server
volumes:
- /Volumes/CaseSensitive/development/wildfly/deployments/:/opt/jboss/wildfly/standalone/deployments/
links:
- mysql:mysql_server
- mongo:mongo_server
- elasticsearch:elasticsearch_server
When I execute docker-compose
everything is ok, but I get a random name for the new image. Is it possible to set a name to the build image?
The easiest way to list Docker images is to use the “docker images” with no arguments. When using this command, you will be presented with the complete list of Docker images on your system. Alternatively, you can use the “docker image” command with the “ls” argument.
Description. An image name is made up of slash-separated name components, optionally prefixed by a registry hostname. The hostname must comply with standard DNS rules, but may not contain underscores. If a hostname is present, it may optionally be followed by a port number in the format :8080 .
A Docker image is a file used to execute code in a Docker container. Docker images act as a set of instructions to build a Docker container, like a template. Docker images also act as the starting point when using Docker.
For docker-compose version 2 file format, you can build and tag an image for one service and then use that same built image for another service.
For my case, I want to set up an elasticsearch cluster with 2 nodes, they both need to use the same image, but configured to run differently. I also want to build my own custom elasticsearch image from my own Dockerfile. So this is what I did (docker-compose.yml
):
version: '2'
services:
es-master:
build: ./elasticsearch
image: porter/elasticsearch
ports:
- "9200:9200"
container_name: es_master
es-node:
image: porter/elasticsearch
depends_on:
- es-master
ports:
- "9200"
command: elasticsearch --discovery.zen.ping.unicast.hosts=es_master
You can see that in the first service definition es-master
, I use the build
option to build an image from the Dockerfile in ./elasticsearch
. I tag the image with the name porter/elasticsearch
with the image
option.
Then, I reference this built image in the es-node
service definition with the image
option, and also use a depends_on
to make sure the other container es-master
is built and run first.
As per docker-compose 1.6.0:
You can now specify both a build and an image key if you're using the new file format.
docker-compose build
will build the image and tag it with the name you've specified, whiledocker-compose pull
will attempt to pull it.
So your docker-compose.yml
would be
version: '2'
services:
wildfly:
build: /path/to/dir/Dockerfile
image: wildfly_server
ports:
- 9990:9990
- 80:8080
To update docker-compose
sudo pip install -U docker-compose==1.6.0
The name of the image generated by docker-compose depends on the folder name by default but you can override it by using --project-name
argument:
$ docker-compose --project-name foo build bar
$ docker images foo_bar
Once docker-compose 1.6.0 is out, you may specify build:
and image:
to have an explicit image name (see arulraj.net's answer).
A third is to create an image from the container:
$ docker-compose up -d bar
$ docker commit $(docker-compose ps -q bar) foo_bar
$ docker-compose rm -f bar
Depending on your use case, you can use an image which has already been created and specify it's name in docker-compose
.
We have a production use case where our CI server builds a named Docker image. (docker build -t <specific_image_name> .
). Once the named image is specified, our docker-compose
always builds off of the specific image. This allows a couple of different possibilities:
1- You can ensure that where ever you run your docker-compose
from, you will always be using the latest version of that specific image.
2- You can specify multiple named images in your docker-compose
file and let them be auto-wired through the previous build step.
So, if your image is already built, you can name the image with docker-compose
. Remove build
and specify image:
wildfly:
image: my_custom_wildfly_image
container_name: wildfly_server
ports:
- 9990:9990
- 80:8080
environment:
- MYSQL_HOST=mysql_server
- MONGO_HOST=mongo_server
- ELASTIC_HOST=elasticsearch_server
volumes:
- /Volumes/CaseSensitive/development/wildfly/deployments/:/opt/jboss/wildfly/standalone/deployments/
links:
- mysql:mysql_server
- mongo:mongo_server
- elasticsearch:elasticsearch_server
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With