I am getting this message
nc command is missing
and by doing some R&D, I got to know that in order to resolve this, (I think) I need to run below command in MySQL container in docker-compose
RUN apt-get -q update && apt-get -qy install netcat
But the issue is I don’t have it’s docker file else I could have written this command in docker file and might have called docker file from docker-compose
Does anyone have any idea how can I run this command from docker-compose?
Edit 1:
I have made separate the DockerFile for mysql which consists of
FROM mysql:8
RUN apt-get -q update && apt-get -qy install netcat
COPY wait-for.sh .
and then called this docker file from docker-compose which goes like this…
version: "3"
services:
mysql-standalone:
image: mysql:8.0.25
environment:
- MYSQL_ROOT_PASSWORD=********
- MYSQL_DATABASE=usermanagementappdp
ports:
- 3306:3306
depends_on: ['eureka-server']
build:
context: "./mysqlDockerFile2"
dockerfile: "Dockerfile"
volumes:
- ./wait-for:/docker-entrypoint-initdb.d
entrypoint: ["/docker-entrypoint-initdb.d/wait-for.sh", "eureka-server:8761", "--", "docker-entrypoint.sh"]
phpmyadmin:
image: phpmyadmin/phpmyadmin:latest
restart: always
environment:
PMA_HOST: mysql-standalone
PMA_USER: root
PMA_PASSWORD: root123M.
ports:
- 8085:80
eureka-server:
image: eureka-server
ports:
- 8761:8761
build:
context: "../Eureka-Server-For-User-Management-App"
dockerfile: "Dockerfile"
usermanagementapp-docker:
image: usermanagementapp-docker:latest
ports:
- 8089:8089
links:
- eureka-server
environment:
EUREKA_CLIENT_SERVICEURL_DEFAULTZONE: http://eureka-server:8761/eureka
SPRING_DATASOURCE_URL: jdbc:mysql://mysql-standalone:3306/usermanagementappdp?allowPublicKeyRetrieval=true&autoReconnect=true&useSSL=false
build:
context: "./"
dockerfile: "Dockerfile"
restart: on-failure
entrypoint: ["/wait-for.sh", "mysql-standalone:3306", "--", "['java','-jar','/app.jar']"]
depends_on: ['mysql-standalone','eureka-server']
Docker file for User management app is:
FROM openjdk:8
Add target/User-Management-App-0.0.1-SNAPSHOT.jar app.jar
VOLUME /tmp
EXPOSE 8089
RUN apt-get -q update && apt-get -qy install netcat
COPY wait-for.sh .
COPY target/User-Management-App-0.0.1-SNAPSHOT.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
Docker file for Eureka- server
FROM openjdk:8
EXPOSE 8761
ADD /target/Eureka-Server-For-User-Management-App-0.0.1-SNAPSHOT.jar netflix-eureka-server-1.0.jar
COPY wait-for.sh .
netflix-eureka-server-1.0.jar
ENTRYPOINT ["java","-jar","netflix-eureka-server-1.0.jar"]
Edit 2
I just edited mysql docker file to check if it is even getting executed or not
RUN echo "'Entered in docker file of mysql'"
FROM mysql:8
RUN apt-get -q update && apt-get -qy install netcat
COPY wait-for.sh .
RUN echo "'Exiting docker file of mysql'"
and found that on doing docker-compose up
it is not echoing the text
3
Answers
You could build and publish your own container image if you wanted with a dockerfile like this
and build it like
docker build . -t user123/mysql:8
and push it like
docker push user123/mysql:8
then switch your docker compose to use your custom container.
if you just need to pop in temporarily to install netcat, you can do that by doing
docker exec -it --user=root ContainerHashOrName /bin/bash
whereContainerHashOrName
can be retrieved fromdocker ps
then just run your commands like you would on any other distro. Just be aware that you are only making changes to the specific instance of a container and that any rescheduling will bring up a different instance of the container.You can use "build" option of compose file, https://docs.docker.com/compose/compose-file/compose-file-v3/#build and use your own
Dockerfile
starts withFROM mysql:8
and then install all additional stuff you need.You don’t need to install
netcat
in the database server container. There are a couple of other things you can clean up to simplify this setup.Let’s start by thinking through what needs to happen when you run
docker-compose up
. The application container can’t function until the database is up and running; for that, the application container is using thewait-for
script, which in turn usesnc
. The database itself doesn’t need to make any outbound connections, though; it needs to start up and accept inbound connections so the rest of the system can proceed. So you don’t neednc
in the database server container, and you can just use the standard unmodifiedmysql
image.(In your Dockerfile you show the database depending on the Eureka service registry; but the database itself won’t do anything to connect to it, and you’re using a direct connection to the database from your application. It doesn’t need to be part of this stack.)
Your Compose setup also overrides the image’s
entrypoint:
. This shouldn’t usually be necessary. I’d suggest a pattern where the image’sENTRYPOINT
is a self-contained script that ends with a shellexec "$@"
command, which will let it run theCMD
passed to it as arguments. So that script could look something likeThen in your application’s Dockerfile — again, you don’t need to change anything in the database’s Dockerfile — set this script as the
ENTRYPOINT
, and make yourjava -jar
command theCMD
.Now in your
docker-compose.yml
setup you can get rid of most of the overrides. Run an unmodifiedmysql
image and don’t override thecommand:
orentrypoint:
of anything.The setup you show above will contaminate your local copy of the MySQL image, so before you start, you should clean it up
If you need to do some sort of registration in the MySQL image at startup time, then you can follow this same basic approach. It is helpful to look up the Docker Hub
mysql
image page and from there its Dockerfile because you will need to know the originalENTRYPOINT
andCMD
.In the
ENTRYPOINT
wrapper script, at the end, run the original entrypoint:In your derived Dockerfile, you’ll need to repeat the original
CMD
In your Compose file, do not specify both
image: mysql
and abuild:
block. This will overwrite your local copy of the Docker Hub image with your custom build. For most purposes you can only specifybuild:
and ignoreimage:
. You do not need to usevolumes:
to inject code, that’s contained within the custom Dockerfile.