How to install sudo and nano command in MySql docker image

Viewed 1715

I am trying to connect Django application with MySql docker container. I am using the latest version of MySql i.e MySql 8.0 to build a container. I was able to build the MySql container successfully but I am not able to connect it using Django's default MySql Connector. When I run the docker-compose up command I get the error mentioned below.

django.db.utils.OperationalError: (1045, 'Plugin caching_sha2_password could not be loaded: /usr/lib/x86_64-linux-gnu/mariadb19/plugin/caching_sha2_password.so: cannot open shared object file: No such file or directory')

I started looking for the solution and got to know that MySql has released a major change in its default authentication plugin which is not support by most of the MySql Connectors.

To fix this issue I will have to set the default-authentication-plugin to mysql_native_password in my.cnf file of MySql container.

I logged into container using command docker exec -it <conatiner id> /bin/bash and was also able to locate the my.cnf file inside the container.

To edit the my.cnf file I will have to use the nano command as stated below.

nano my.cnf

But unfortunately nano command is not installed in MySql Container. To install nano I will need sudo installed in container.

I tried installing sudo using below mentioned command but it did not work.

apt-get install sudo

error -

Reading package lists... Done
Building dependency tree       
Reading state information... Done

What are the possible solutions to fix this issue.

2 Answers

The issue is not with sudo because you've already permissions to install pacakegs. You should instead update package manager before to install new packages in order to update package repositories:

RUN apt-get update
RUN apt-get install nano

In general you shouldn't try to directly edit files in containers. Those changes will get lost as soon as the container is stopped and deleted; this happens extremely routinely since many Docker options can only be set at startup time, and the standard way to update the software in a container is to recreate it with a newer image. In your case, you also can't live-edit a configuration file the main container process needs at startup time, because it will have already read the configuration file by the time you're able to edit it.

The right way to do this is to inject the modified configuration file at container startup time. If you haven't already, get the default configuration file out of the image

docker run --rm mysql:8 cat /etc/mysql/my.cnf > my.cnf

and edit it, directly, on your host, using your choice of text editor. When you launch the container, inject the modified file

docker run -v $PWD/my.cnf:/etc/mysql/my.cnf ... mysql:8

or, in Docker Compose,

volumes:
  - ./my.cnf:/etc/mysql/my.cnf

The Docker Hub mysql image documentation has some more suggestions on ways to set this; see "Using a custom MySQL configuration file" there.

While docker exec is an extremely useful debugging tool, it shouldn't be part of your core workflow, and I'd recommend trying to avoid it in cases like this. (With the bind-mount approach, you can add the modified config file to your source control system and blindly docker-compose up like normal without knowing this detail; a docker exec approach you'd have to remember and repeat by hand every time you started the container stack.)

Also note that you don't need sudo in Docker at all. Every context where you can run something (Dockerfiles, docker run, docker exec) has some way to explicitly specify the user ID, so you can docker exec -u root .... sudo generally depends on things like users having passwords and interactive prompting, which works well for administering a real Linux host but doesn't match a typical Docker environment.

Related